package com.jy.modules.cims.component.common.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.DictDeatil;
import com.jy.modules.cims.data.common.dao.TCMmLoanDetailDao;
import com.jy.modules.cims.data.common.dto.TCMmLoanDetailDTO;
import com.jy.platform.core.common.BaseDTO;

/**
 * @classname: TCMmLoanDetailService
 * @description: 定义  贷款明细 实现类
 * @author:  zhiqiang
 */
@Service("com.jy.modules.cims.component.common.service.TCMmLoanDetailService")
public class TCMmLoanDetailService  {
	
	@Autowired
	private TCMmLoanDetailDao dao;
	
	@Autowired
	private DictDeatil dict;

	/**
     * @author zhiqiang
     * @description: 分页查询 贷款明细列表
     * @date 2015-01-12 20:32:17
     * @param searchParams 条件
     * @return
     * @throws
     */ 
	public List<TCMmLoanDetailDTO> searchTCMmLoanDetailByPaging(Map<String,Object> searchParams) throws Exception {
		List<TCMmLoanDetailDTO> dataList =  dao.searchTCMmLoanDetailByPaging(searchParams);
		return dataList;
	}
	
	/**
     * @author zhoupeng  searchTCMmLoanDetailExport
     * @description: 导出贷款明细列表 查询
     * @date 2015-10-31 20:32:17
     * @param searchParams 条件
     * @return
     * @throws
     */ 
	public List<TCMmLoanDetailDTO> searchTCMmLoanDetailExport(Map<String,Object> searchParams) throws Exception {
		List<TCMmLoanDetailDTO> dataList = dao.searchTCMmLoanDetailExport(searchParams);
		String[] columnCodes = new String[] { "C_CL_LOAN_STATUS", "LOANPURPOSE", "cooperorgCodes" };

		Map<String, Map> excelColumnsConvertMap = dict.queryDictDetailByTempalteMap(columnCodes);

		List<TCMmLoanDetailDTO> loanDetaillist = new ArrayList<TCMmLoanDetailDTO>();
		for (TCMmLoanDetailDTO loanDetailDto : dataList) {
			loanDetailDto.setCooperorgName(excelColumnsConvertMap.get("cooperorgCodes").get(loanDetailDto.getCooperorgCode()).toString());// 合作机构
			loanDetailDto.setLoanPurposeName(excelColumnsConvertMap.get("LOANPURPOSE").get(loanDetailDto.getLoanPurpose()).toString());// 借款用途
																																		// loanPurpose
			loanDetailDto.setLoanStatusName(excelColumnsConvertMap.get("C_CL_LOAN_STATUS").get(loanDetailDto.getLoanStatus()).toString());// 贷款状态

			if (loanDetailDto.getRiskCustomerUpdateTime() == null) {
				loanDetailDto.setRiskCustomerYn("否");
			} else {
				loanDetailDto.setRiskCustomerYn("是");
			}

			loanDetaillist.add(loanDetailDto);
		}
		return loanDetaillist;
    }
	
	
	
	
	/**
     * @author zhiqiang
     * @description: 按条件查询贷款明细列表
     * @date 2015-01-12 20:32:17
     * @param searchParams 条件
     * @return
     * @throws
     */
	public List<TCMmLoanDetailDTO> searchTCMmLoanDetail(Map<String,Object> searchParams) throws Exception {
	    List<TCMmLoanDetailDTO> dataList = dao.searchTCMmLoanDetail(searchParams);
        return dataList;
    }
	/**
     * @author zhiqiang
     * @description: 查询贷款明细对象
     * @date 2015-01-12 20:32:17
     * @param id
     * @return
     * @throws
     */ 
	public TCMmLoanDetailDTO queryTCMmLoanDetailByPrimaryKey(String id) throws Exception {
		
		TCMmLoanDetailDTO dto = dao.findTCMmLoanDetailByPrimaryKey(id);
		
		if(dto == null) dto = new TCMmLoanDetailDTO();
		
		return dto;
		
	}

	/**
     * @title: insertTCMmLoanDetail
     * @author zhiqiang
     * @description: 新增 贷款明细对象
     * @date 2015-01-12 20:32:17
     * @param dto
     * @return
     * @throws
     */
	@SuppressWarnings("all")
	public Long insertTCMmLoanDetail(TCMmLoanDetailDTO dto) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
		
		int count = dao.insertTCMmLoanDetail(paramMap);
		
		TCMmLoanDetailDTO resultDto = (TCMmLoanDetailDTO) paramMap.get("dto");
		Long keyId = resultDto.getId();
		return keyId;
	}
	/**
     * @title: updateTCMmLoanDetail
     * @author zhiqiang
     * @description: 修改 贷款明细对象
     * @date 2015-01-12 20:32:17
     * @param paramMap
     * @return
     * @throws
     */
	public void updateTCMmLoanDetail(TCMmLoanDetailDTO dto) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
		
		dao.updateTCMmLoanDetail(paramMap);
	}
	
	/**
	 * 更新贷款明细通过条件
	 * @param dto
	 * @throws Exception
	 */
	public void updateTCMmLoanDetailByCondition(TCMmLoanDetailDTO dto) throws Exception {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
		
		dao.updateTCMmLoanDetailByCondition(paramMap);
	}
	
	/**
     * @title: updateTCMmLoanDetail
     * @author zhiqiang
     * @description: 修改 贷款明细对象
     * @date 2015-01-12 20:32:17
     * @param paramMap
     * @return
     * @throws
     */
	public TCMmLoanDetailDTO queryTCMmLoanDetailByContractNo(String contractNo) throws Exception {
		
		TCMmLoanDetailDTO tCMmLoanDetailDTO =  dao.findTCMmLoanDetailByPrimaryContractNo(contractNo);
		
		 return tCMmLoanDetailDTO;
	}
	
	/**
     * @title: deleteTCMmLoanDetailByPrimaryKey
     * @author zhiqiang
     * @description: 删除 贷款明细,按主键
     * @date 2015-01-12 20:32:17
     * @param paramMap
     * @throws
     */ 
	public void deleteTCMmLoanDetailByPrimaryKey(BaseDTO baseDto,String ids) throws Exception {
		if(SimpleStringUtils.isEmpty(ids)) throw new Exception("删除失败！传入的参数主键为null");
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("ids", ids);
		dao.deleteTCMmLoanDetailByPrimaryKey(paramMap);
	}

}

